﻿using System;
using System.Linq;
using System.Reflection;
using System.Xml;
using Headsight.BPMN2WF.BPMN.Model;

namespace Headsight.BPMN2WF.Transformation.BPMN2TransformerMappings.Config
{
    public class BPMN2WFTransformationConfigMapCondition
    {
        private const string BPMNAssemblyName = "Headsight.BPMN2WF.BPMN, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e236fd441f43256f";
        public static Assembly bpmnAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == BPMNAssemblyName);

        public ConditionSource Source { get; set; }
        public Type Value { get; set; }

        public bool Matches(BPMNFlowNode node, Type typeOfFlowElement)
        {
            if (Source == ConditionSource.FlowNode)
                return (Value.IsInstanceOfType(node));
            return (Value.IsAssignableFrom(typeOfFlowElement));
        }

        public static BPMN2WFTransformationConfigMapCondition FromXmlConditionNode(XmlNode conditionNode)
        {
            var source = (ConditionSource)Enum.Parse(typeof(ConditionSource), conditionNode.Attributes["source"].Value, true);
            return new BPMN2WFTransformationConfigMapCondition
            {
                Source = source,
                Value = bpmnAssembly.GetType(conditionNode.Attributes["value"].Value)
            };
        }
    }

}
